package com.hundredapp.oa.web.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaSlow;
import com.hundredapp.oa.repository.SlowManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.repository.SubjectManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.SlowVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Slow extends ActionSupport {
	
	private List<SlowVO> root;
	
	private Object success;
	private String msg;
	private int id;
	private int studentid;
	private int subjectid;
	private int start;
	private int limit;
	private int total;
	private String studentname;
	private String subjectname;
	private OaSlow oaslow;

	@Autowired
	private SlowManager slowmng;
	@Autowired
	private StudentManager studentmng;
	@Autowired
	private SubjectManager subjectmng;
	
	public String all() {
		String condition = SelectType.SELECT_ALL;
		if(studentname != null) {
			studentname = JSEscape.unescape(studentname);
			if(studentname!=null)
				condition += " and oastudents.displayName like '%" +studentname+"%' ";
		}
		if(subjectname != null) {
			subjectname = JSEscape.unescape(subjectname);
			if(subjectname!=null)
				condition += " and oasubjects.displayName like '%" +subjectname+"%' ";
		}
		List<OaSlow> slows = new ArrayList<OaSlow>();
		slows = slowmng.findSlows(condition, start, limit);
		root = new ArrayList<SlowVO>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(OaSlow slow : slows){
			SlowVO slowvo = new SlowVO();
			slowvo.setId(slow.getId());
			slowvo.setStudentName(slow.getOastudents().getDisplayName());
			slowvo.setSubjectName(slow.getOasubjects().getDisplayName());
			slowvo.setSlowtime(sdf.format(slow.getSlowstart()) + "~~" + sdf.format(slow.getSlowend()));
			slowvo.setSlowlesson(slow.getSlowlesson());
			slowvo.setAddtime(sdf.format(slow.getAddstart()) + "~~" + sdf.format(slow.getAddend()));
			slowvo.setAddlesson(slow.getAddlesson());
			if(slow.getChargeback() == 1)
				slowvo.setChargeback("扣除占位费");
			else
				slowvo.setChargeback("不扣占位费");
			root.add(slowvo);
		}
		return SUCCESS;
	}
	
	public String findbystu(){
		List<OaSlow> slows = slowmng.findSlows(" oastudents.id ="+studentid, start, limit);
		root = new ArrayList<SlowVO>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(OaSlow slow:slows){
			SlowVO slowvo = new SlowVO();
			slowvo.setId(slow.getId());
			slowvo.setStudentName(slow.getOastudents().getDisplayName());
			slowvo.setSubjectName(slow.getOasubjects().getDisplayName());
			slowvo.setSlowtime(sdf.format(slow.getSlowstart()) + "~~" + sdf.format(slow.getSlowend()));
			slowvo.setSlowlesson(slow.getSlowlesson());
			slowvo.setAddtime(sdf.format(slow.getAddstart()) + "~~" + sdf.format(slow.getAddend()));
			slowvo.setAddlesson(slow.getAddlesson());
			if(slow.getChargeback() == 1)
				slowvo.setChargeback("扣除占位费");
			else
				slowvo.setChargeback("不扣占位费");
			root.add(slowvo);
		}
		return SUCCESS;
	}
	
	public String add(){
		oaslow.setCreateUserId(WebUtil.GetLoginUserId());
		oaslow.setOastudents(studentmng.findById(studentid));
		oaslow.setOasubjects(subjectmng.findById(subjectid));
		slowmng.add(oaslow);
		success=true;
		msg="添加成功";
		return SUCCESS;
	}
	
	public String find(){
		oaslow = slowmng.findSlowById(id);
		return SUCCESS;
	}
	
	public String update(){
		OaSlow slow = slowmng.findSlowById(id);
		slow.setSlowstart(oaslow.getSlowstart());
		slow.setSlowend(oaslow.getSlowend());
		slow.setSlowlesson(oaslow.getSlowlesson());
		slow.setAddstart(oaslow.getAddstart());
		slow.setAddend(oaslow.getAddend());
		slow.setAddlesson(oaslow.getAddlesson());
		slow.setChargeback(oaslow.getChargeback());
		System.out.println("----"+oaslow.getChargeback());
		slow.setOastudents(studentmng.findById(studentid));
		slow.setOasubjects(subjectmng.findById(subjectid));
		slowmng.update(slow);
		success=true;
		msg="修改成功";
		return SUCCESS;
	}

	public List<SlowVO> getRoot() {
		return root;
	}

	public void setRoot(List<SlowVO> root) {
		this.root = root;
	}

	public Object getSuccess() {
		return success;
	}

	public void setSuccess(Object success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public OaSlow getOaslow() {
		return oaslow;
	}

	public void setOaslow(OaSlow oaslow) {
		this.oaslow = oaslow;
	}

	public int getStudentid() {
		return studentid;
	}

	public void setStudentid(int studentid) {
		this.studentid = studentid;
	}

	public int getSubjectid() {
		return subjectid;
	}

	public void setSubjectid(int subjectid) {
		this.subjectid = subjectid;
	}

	public String getStudentname() {
		return studentname;
	}

	public void setStudentname(String studentname) {
		this.studentname = studentname;
	}

	public String getSubjectname() {
		return subjectname;
	}

	public void setSubjectname(String subjectname) {
		this.subjectname = subjectname;
	}
}
